package cn.edu.ctbu.datajpa01.webapi;

import cn.edu.ctbu.datajpa01.core.PageUtils;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.domain.Teacher;
import cn.edu.ctbu.datajpa01.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RestController
@RequestMapping("/webapi/teacher")
public class TeacherRestController {

    @Autowired
    TeacherService teacherService;

    @GetMapping("/list")
    public List<Teacher> getAll(){
        List<Teacher> teachers=teacherService.findAll();

        return teachers;
    }

    /**
     * 分页都区全部内容
     */
//    @GetMapping("/getbypage")
//    public PageUtils getByPage(@RequestParam(value="page",defaultValue = "0") Integer page,
//                               @RequestParam(value="size",defaultValue = "10")Integer size){
//        Pageable pageable= PageRequest.of(page,size, Sort.by("id").descending());
//        Page<Student> studentPage=studentService.findAll(pageable);
//
//      PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
//        return pageUtils;
//    }


    /**
     * 分页过滤查询
     * @return
     */
    @GetMapping("/getbypage")
    public PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                               @RequestParam(value = "size",defaultValue = "10") Integer size,
                               @RequestParam(value = "name",defaultValue = "") String name,
                               @RequestParam(value = "no",defaultValue = "") String no)
    {
//        Pageable pageable= PageRequest.of(page,size,Sort.by("id").descending());
        Page<Teacher> teacherPage;
//                =studentService.findAll(pageable);
        //参数为空，正常查询
        if (StringUtils.isEmpty(name)&&StringUtils.isEmpty(no)){
            Pageable pageable= PageRequest.of(page,size, Sort.by("id").descending());
            teacherPage=teacherService.findAll(pageable);

        }
        //匹配学号查询
        else  if (StringUtils.hasText(no))  {
            Teacher teacher=new Teacher();
            teacher.setNo(no);
            Pageable pageable=  PageRequest.of(0,3, Sort.by("no").descending());
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());//全部模糊查询，即%{no)%
            // 通过Example构建查询条件  特别注意这个Example对象是spring data中的依赖，别引成了hibernate的
            Example<Teacher> example=Example.of(teacher,matcher);

            teacherPage=teacherService.findAll(example,pageable);

        }
        //匹配姓名查询
        else  {
            Teacher teacher=new Teacher();
            teacher.setName(name);
            Pageable pageable=  PageRequest.of(0,3, Sort.by("id").ascending());
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Teacher> example=Example.of(teacher,matcher);
            teacherPage=teacherService.findAll(example,pageable);
        }


        PageUtils pageUtils=new PageUtils(teacherPage.getContent(), teacherPage.getTotalElements());
        return pageUtils;
    }

    /**
     * 新增学生的方法，/webapi/student/insert
     * @param teacher
     * @return
     */
    @PostMapping("/insert")
    public Teacher insert(Teacher teacher){

        Teacher teacher1 = teacherService.insert(teacher);
        return teacher1;

    }




    /**
     * 更新学生的方法，/webapi/student/update
     * @param teacher
     * @return
     */
    @PutMapping("/update")
    public Teacher update(Teacher teacher){
        //读取旧数据
        Teacher oldteacher=teacherService.getById(teacher.getId());
        if(StringUtils.isEmpty(teacher.getPassword())){
            //没有新密码，那么就是旧密码
            teacher.setPassword(oldteacher.getPassword());
        }
        teacher = teacherService.update(teacher);
        return teacher;

    }


    /**
     * 删除学生的方法，/webapi/student/delete
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
//    通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中：URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。
    public void delete(@PathVariable  Integer id){

        Teacher byId = teacherService.getById(id);
        teacherService.delete(id);


    }
    /**
     * 读取的url:/webapi/student/get/id
     * @return
     */

    @GetMapping("/get/{id}")
    public Teacher get(@PathVariable Integer id){
        Teacher teacher=teacherService.getById(id);
        teacher.setPassword("");
        return teacher;
    }

}
